package info.jafe.common.log;

import android.support.annotation.NonNull;
import android.util.Log;

import info.jafe.common.util.AppUtils;

/**
 * Created by jafe chang on 2016/12/10.
 */

public class LogUtils {
    private static final boolean isDebuggable = AppUtils.isDebuggable();

    public static void d(String msg) {
        if (!isDebuggable) {
            return;
        }
        String[] tm = combine(getStackTraceElement(), msg);
        d(tm[0], tm[1]);
    }

    public static void d(String tag, String msg) {
        if (!isDebuggable) {
            return;
        }
        if (tag == null) {
            tag = "";
        }
        Log.d(tag, msg);
    }

    public static void e(String msg) {
        if (!isDebuggable) {
            return;
        }
        String[] tm = combine(getStackTraceElement(), msg);
        e(tm[0], tm[1]);
    }

    public static void e(String tag, String msg) {
        if (!isDebuggable) {
            return;
        }
        if (tag == null) {
            tag = "";
        }
        Log.e(tag, msg);
    }

    public static void i(String msg) {
        if (!isDebuggable) {
            return;
        }
        String[] tm = combine(getStackTraceElement(), msg);
        i(tm[0], tm[1]);
    }

    public static void i(String tag, String msg) {
        if (!isDebuggable) {
            return;
        }
        if (tag == null) {
            tag = "";
        }
        Log.i(tag, msg);
    }

    public static void w(String msg) {
        if (!isDebuggable) {
            return;
        }
        String[] tm = combine(getStackTraceElement(), msg);
        w(tm[0], tm[1]);
    }

    public static void w(String tag, String msg) {
        if (!isDebuggable) {
            return;
        }
        if (tag == null) {
            tag = "";
        }
        Log.w(tag, msg);
    }

    public static void v(String msg) {
        if (!isDebuggable) {
            return;
        }
        String[] tm = combine(getStackTraceElement(), msg);
        v(tm[0], tm[1]);
    }

    public static void v(String tag, String msg) {
        if (!isDebuggable) {
            return;
        }
        if (tag == null) {
            tag = "";
        }
        Log.v(tag, msg);
    }

    private static String[] combine(StackTraceElement stackTraceElement, String msg) {
        String[] result = new String[2];
        if (stackTraceElement == null) {
            result[0] = "";
            result[1] = msg;
        } else {
            result[0] = getTag(stackTraceElement);
            result[1] = getMsg(stackTraceElement, msg);
        }
        return result;
    }

    private static String getTag(@NonNull StackTraceElement stackTraceElement) {
        return stackTraceElement.getFileName();
    }

    private static String getMsg(@NonNull StackTraceElement stackTraceElement, String msg) {
        return "[" + stackTraceElement.getMethodName() + "](" + stackTraceElement.getLineNumber() + ") " + msg;
    }

    private static StackTraceElement getStackTraceElement() {
        Exception e = new Exception();
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        if (stackTraceElements == null || stackTraceElements.length < 2) {
            return null;
        }
        return stackTraceElements[2];
    }
}
